package com.lihui;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 功能描述
 *
 * @author wileda
 * @date 2023/04/02  18:27
 */
@WebServlet("/json")
public class JsonServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置一下请求的编码集
        req.setCharacterEncoding("utf-8");

        // 解析body
        byte[] bytes = new byte[req.getContentLength()];
        req.getInputStream().read(bytes);
        // 能过byte数组来创建一个String对象
        String json = new String(bytes, "utf-8");
        // 打印这个JSON字符串
        System.out.println(json);
        // 原样返回
        resp.setContentType("application/json; charset=utf-8");
        resp.getWriter().write(json);

    }
}
